var Scroll = function() {
    this.myScroll = null;
    this.loadingStep = 0; //加载状态0默认，1显示加载状态，2执行加载数据，只有当为0时才能再次加载，这是防止过快拉动刷新

    this.loaded = function (idname, pagePullDown, pagePullUp) {
    var myLoader = this;
    this.pullDownEl = document.getElementById(idname + '-pull-down');
    this.pullDownL = document.getElementById(idname + '-pull-down-label');
    this.pullDownEl['class'] = this.pullDownEl.className;

    this.pullUpEl = document.getElementById(idname + '-pull-up');
    this.pullUpL = document.getElementById(idname + '-pull-up-label');
    this.pullUpEl['class'] = this.pullUpEl.className;

    this.btnTop = document.getElementById("btn-top");

    this.myScroll = new IScroll('#' + idname + '-wrapper', {
        probeType: 1, //probeType：1对性能没有影响。在滚动事件被触发时，滚动轴是不是忙着做它的东西。probeType：2总执行滚动，除了势头，反弹过程中的事件。这类似于原生的onscroll事件。probeType：3发出的滚动事件与到的像素精度。注意，滚动被迫requestAnimationFrame（即：useTransition：假）。
        scrollbars: false, //有滚动条
        mouseWheel: false, //允许滑轮滚动
        fadeScrollbars: false, //滚动时显示滚动条，默认影藏，并且是淡出淡入效果
        bounce: true, //边界反弹
        interactiveScrollbars: true, //滚动条可以拖动
        shrinkScrollbars: 'scale', // 当滚动边界之外的滚动条是由少量的收缩。'clip' or 'scale'.
        click: true, // 允许点击事件
        keyBindings: true, //允许使用按键控制
        momentum: true // 允许有惯性滑动
    });

  //滚动时
    this.myScroll.on('scroll', function() {
        if (myLoader.loadingStep === 0 && !myLoader.pullDownEl.className.match('flip') && !myLoader.pullUpEl.className.match('flip')) {
            if (this.y > 5) {
                //下拉刷新效果
                myLoader.pullDownEl.style.display = 'BLOCK';
                myLoader.myScroll.refresh();
                myLoader.pullDownEl.className += ' flip';
                myLoader.pullDownL.innerHTML = '准备刷新...';

                myLoader.loadingStep = 1;
            } else if (this.y < (this.maxScrollY - 5)) {
                //上拉加载效果
                myLoader.pullUpEl.style.display = 'BLOCK';
                myLoader.myScroll.refresh();
                myLoader.pullUpEl.className += ' flip';
                myLoader.pullUpL.innerHTML = '准备加载...';
                myLoader.loadingStep = 1;
            }
        }
        
    });
    
     //滚动完毕
    this.myScroll.on('scrollEnd', function() {
        if (myLoader.loadingStep == 1) {
            if (myLoader.pullDownEl.className.match('flip')) {
                myLoader.pullDownEl.className += ' loading';
                myLoader.pullDownL.innerHTML = 'Loading...';
                myLoader.loadingStep = 2;
                pagePullDown();     //执行刷新
                setTimeout(function() {
                    console.log('触发了下拉刷新');
                    myLoader.pullDownEl.className = 'pull-down';
                    myLoader.pullDownEl.style.display = 'NONE';
                    myLoader.loadingStep = 0;
                    myLoader.myScroll.refresh();
                }, 1500);

            } else if (myLoader.pullUpEl.className.match('flip')) {
                myLoader.pullUpEl.className += ' loading';
                myLoader.pullUpL.innerHTML = 'Loading...';
                myLoader.loadingStep = 2;
                pagePullUp();       //加载更多
                setTimeout(function() {
                    console.log('触发了上拉加载事件');
                    myLoader.pullUpEl.className = 'pull-up';
                    myLoader.pullUpEl.style.display = 'NONE';
                    myLoader.loadingStep = 0;
                    myLoader.myScroll.refresh();
                }, 1500);
            }
        }
        if(myLoader.btnTop && myLoader.myScroll.y < -200){
            myLoader.btnTop.style.display="block";
        } else if(myLoader.btnTop) {
            myLoader.btnTop.style.display="none";
        }
    });
  
  };
};

